from django.shortcuts import render,HttpResponse,redirect,reverse,get_object_or_404,get_list_or_404
from rbac.models import Menu,Permission
from rbac.forms.menu import MenuModelForm
from rbac.forms.permission import SecondMenuModelForm
from django.http import QueryDict
from rbac.service.urls import memory_reverse_url

#only modify userinfo name password email,but not roles


def menu_list(request):
    """
    menu and permissions list
    :param request:
    :return:
    """
    menus=Menu.objects.all()
    menu_id=request.GET.get('mid',None) #用户选择的一级菜单
    second_menu_id=request.GET.get('sid',None)#用户选择的二级菜单
    #先判断下mid sid是否是合法存在的
    menu_exist=Menu.objects.filter(id=menu_id).exists()
    second_menu_exist=Permission.objects.filter(id=second_menu_id).exists()
    if not menu_exist:
        menu_id=None

    if not second_menu_exist:
        second_menu_id=None

    #如果menu_id合法且存在，再走下面的流程
    second_menus={}
    permissions={}
    if menu_id:
        second_menus=Permission.objects.filter(menu_id=menu_id)
        if second_menu_id:
            permissions=Permission.objects.filter(pid=second_menu_id)

    # second_menus2=Menu.objects.filter(pk=menu_id).permission_set.all()
    # print(second_menus)
    # print(second_menus2)
    context = {
        'menus':menus,
        'menu_id':menu_id,
        'second_menu_id':second_menu_id,
        'second_menus':second_menus,
        "permissions":permissions,
    }

    return render(request,'rbac/menu_list.html',context)


def menu_add(request):
    #增加一个保留原搜索条件的功能，在添加完新菜单后，能回到原来的激活的菜单中
    form=MenuModelForm()
    if request.method == "POST":
        form = MenuModelForm(data=request.POST)
        if form.is_valid():
            form.save()
            return redirect(memory_reverse_url(request,'rbac:menu_list'))
    context = {
        'form':form,
    }
    return render(request,'rbac/change.html',context)


def menu_edit(request,pk):
    #仅修改用户名和邮箱，不修改密码
    obj = get_object_or_404(Menu, pk=pk)
    if request.method == 'GET':
        form = MenuModelForm(instance=obj)
        return render(request, 'rbac/change.html', {'form': form})
    form = MenuModelForm(data=request.POST, instance=obj)
    if form.is_valid():
        form.save()
        return redirect(memory_reverse_url(request,'rbac:menu_list'))
    return render(request, 'rbac/change.html', {'form': form})


def menu_del(request,pk):
    origin_url = memory_reverse_url(request,'rbac:menu_list')
    if request.method == "GET":
        return render(request, 'rbac/delete.html', {'cancel': origin_url})
    Menu.objects.filter(pk=pk).delete()
    return redirect(origin_url)


def second_menu_add(request):
    #二级菜单添加
    query_dict=QueryDict(request.GET.get('_filter'))
    mid=query_dict.get('mid',None)
    # sid=query_dict.get('sid',None)
    # print(query_dict)
    # print('mid',mid,"sid",sid)
    menu_object=Menu.objects.filter(pk=mid).first()
    if not menu_object:
        menu_object=None
    #menu的初始默认值
    form=SecondMenuModelForm(initial={'menu':menu_object})
    if request.method == "POST":
        form = SecondMenuModelForm(data=request.POST)
        if form.is_valid():
            form.save()
            return redirect(memory_reverse_url(request,'rbac:menu_list'))
    context = {
        'form':form,
    }
    return render(request,'rbac/change.html',context)


def second_menu_edit(request,pk):
    # query_dict = QueryDict(request.GET.get('_filter'))
    # mid = query_dict.get('mid', None)
    # sid=query_dict.get('sid',None)
    # print(query_dict)
    # print('mid',mid,"sid",sid)
    # menu_object = Menu.objects.filter(pk=mid).first()
    # form = SecondMenuModelForm(initial={'menu': menu_object})
    obj = get_object_or_404(Permission, pk=pk)
    if request.method == 'GET':
        form = SecondMenuModelForm(instance=obj)
        return render(request, 'rbac/change.html', {'form': form})
    form = SecondMenuModelForm(data=request.POST, instance=obj)
    if form.is_valid():
        form.save()
        return redirect(memory_reverse_url(request, 'rbac:menu_list'))
    return render(request, 'rbac/change.html', {'form': form})


def second_menu_del(request,pk):
    origin_url = memory_reverse_url(request, 'rbac:menu_list')
    if request.method == "GET":
        return render(request, 'rbac/delete.html', {'cancel': origin_url})
    Permission.objects.filter(pk=pk).delete()
    return redirect(origin_url)
